package 力扣热题100;

import java.util.Comparator;
import java.util.PriorityQueue;

public class LeetCode4 {
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        PriorityQueue<Integer>little=new PriorityQueue<>();
        PriorityQueue<Integer>big=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for (int i = 0; i < nums1.length; i++) {
            big.add(nums1[i]);
            little.add(big.poll());
            if(little.size()-big.size()>1){
                big.add(little.poll());
            }
        }
        for (int i = 0; i < nums2.length; i++) {
            big.add(nums2[i]);
            little.add(big.poll());
            if(little.size()-big.size()>1){
                big.add(little.poll());
            }
        }
        double a=little.peek();
        if((nums1.length+nums2.length)%2==0){
            return a=(a+big.peek())/2.0;
        }
        return a;
    }

    public static void main(String[] args) {
        int []arr= {1,2};
        int[]arr2={3,4};
        findMedianSortedArrays(arr,arr2);
    }
}
